package com.tree.jobquartz.job;


import com.tree.jobquartz.template.SchedulerTemplate;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @ClassName BackTestRequestJob
 * @Description: 执行请求判断是否已经生成回测结果
 * @Author: myl
 * @create_time: 2020/10/23 14:42
 */
@Slf4j
@DisallowConcurrentExecution
public class BackTestRequestJob implements Job {

    @Autowired
    private SchedulerTemplate schedulerTemplate;



    /**
     * 查询回测结果状态定时任务
     *
     * @param context 定时任务上下文，携带定时任务和触发器中绑定的数据信息
     */
    @Override
    public void execute(JobExecutionContext context) {
        // 从上下文中解析信息触发器中携带参数
        String backTestId = (String) context.getTrigger().getJobDataMap().get("backTestId");

        System.out.println("开始执行任务，"+backTestId+"-"+Thread.currentThread().getName());
        // 先暂停定时任务，避免该方法仍在进行中，调度中心再次开启线程执行该方法
//        schedulerTemplate.shutdown(backTestId);

        int count = 0;
//
        while (true){
            System.out.println("backTestId="+backTestId+","+new Date().toString()+Thread.currentThread().getName());
            if (count > 3){
                System.out.println("超过三次"+"-"+Thread.currentThread().getName());
//                schedulerTemplate.resume(backTestId);
                return;
            }
            count++;
            try {

                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }



    }
}
